﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using OpenTK.Audio;
using OpenTK.Audio.OpenAL;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace ArtemisTest
{

    public interface IInputScript
    {
        void Update(double dt);
        void Init();
    }

    //MOUSE PRESSED / RELEASED 
    public abstract class MouseButtonScript : Component, IInputScript
    {
        protected SystemsManager _manager;
        public abstract void Update(double dt);
        public abstract void Init();
        public abstract void OnMouseButtonEvent(object sender, MouseButtonEventArgs e, bool wasPressed);
        public MouseButtonScript(long uid, SystemsManager sys) : base(uid, ComponentType.mouse_button_script) { _manager = sys; Init(); }
    }

    //MOUSE MOVED
    public abstract class MouseMoveScript : Component,IInputScript
    {
        protected SystemsManager _manager;
        public abstract void Update(double dt);
        public abstract void Init();
        public abstract void OnMouseMoveEvent(object sender, MouseMoveEventArgs e);
        public MouseMoveScript(long uid, SystemsManager sys) : base(uid, ComponentType.mouse_move_script) { _manager = sys; Init(); }
    }

    //KEYBOARD PRESSED
    public abstract class KeyboardKeyScript : Component,IInputScript
    {
        protected SystemsManager _manager;
        public abstract void Update(double dt);
        public abstract void Init();
        public abstract void OnKeyboardKeyEvent(object sender, KeyboardKeyEventArgs e, bool wasPressed);
        public KeyboardKeyScript(long uid, SystemsManager sys) : base(uid, ComponentType.keyboard_key_script) { _manager = sys; Init(); }
    }


    #region content

    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
    //Redirect function calls through PhysicsSystem//
    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//

    public class KeyboardKeyScript_MoveWithVel : KeyboardKeyScript
    {
        bool leftHeld = false;
        bool rightHeld = false;
        bool upHeld = false;
        bool downHeld = false;

        public Vector2 GetOffset()
        {
            int dx = 0;
            int dy = 0;
            if (leftHeld) { dx--; }
            if (rightHeld) { dx++; }
            if (upHeld ){dy--;}
            if (downHeld) {dy++;}
            return new Vector2(dx, dy);
        }

        public override void OnKeyboardKeyEvent(object sender, KeyboardKeyEventArgs e, bool wasPressed)
        {
            //Console.WriteLine("Player pressed input event");
            if (e.Key == Key.Left)
            {
                if (wasPressed) { leftHeld = true; } else { leftHeld = false; }
            }
            if (e.Key == Key.Right)
            {
                if (wasPressed) { rightHeld = true; } else { rightHeld = false; }
            }
            if (e.Key == Key.Up)
            {
                if (wasPressed) { upHeld = true; } else { upHeld = false; }
            }
            if (e.Key == Key.Down)
            {
                if (wasPressed) { downHeld = true; } else { downHeld = false; }
            }
        }

        public override void Update(double dt)
        {
            if (dt < 0.0001f) { dt = 0.0001f; } //?
            Vector2 of = GetOffset();
            _manager.physicsSystem.MoveEntity(UID, of.X, of.Y, dt);
        }

        public override void Init()
        {
            
        }

        public KeyboardKeyScript_MoveWithVel(long uid, SystemsManager sys) : base(uid, sys) { }
    }

    #endregion
}
